package com.example.tourmemory.data.getdata;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.example.tourmemory.data.record.RecordData;

import java.util.ArrayList;

public class ScreenShot {
    public static final int TRANSPARENT = 0x00000000;
    public static final int WHITE = 0xffffffff;
    public static final int BLACK = 0xff000000;

    // 获取视图的屏幕截图，指定背景色
    public static Bitmap GetScreenShot(View view, int backgroundColor){
        Drawable background = view.getBackground();
        view.setBackgroundColor(backgroundColor);
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        view.setBackground(background);
        return bitmap;
    }

    // 纵向拼接多个Bitmap
    public static Bitmap ConcatenateBitmapVertical(ArrayList<Bitmap> bitmaps){
        int width = 0;
        for(Bitmap bitmap : bitmaps){
            if(bitmap.getWidth() > width){
                width = bitmap.getWidth();
            }
        }
        int height = 0;
        for (Bitmap bitmap : bitmaps) {
            height += bitmap.getHeight();
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        int top = 0;
        for (Bitmap bmp : bitmaps) {
            canvas.drawBitmap(bmp, 0, top, null);
            top += bmp.getHeight();
        }
        return bitmap;
    }

    // 横向拼接多个Bitmap
    public static Bitmap ConcatenateBitmapHorizontal(ArrayList<Bitmap> bitmaps){
        int width = 0;
        int height = 0;
        for (Bitmap bitmap : bitmaps) {
            if(bitmap.getHeight() > height){
                height = bitmap.getHeight();
            }
            width += bitmap.getWidth();
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        int left = 0;
        for (Bitmap bmp : bitmaps) {
            canvas.drawBitmap(bmp, left, 0, null);
            left += bmp.getWidth();
        }
        return bitmap;
    }

    // 拼接图片九宫格
    public static Bitmap ConcatenateBitmapGrid(ArrayList<Bitmap> bitmaps, int rawSize, int columnSize){
        ArrayList<Bitmap> rawBitmaps = new ArrayList<>();
        for (int i = 0; i < rawSize; ++i){
            ArrayList<Bitmap> columnBitmaps = new ArrayList<>();
            for (int j = 0; j < columnSize; ++j){
                int index = i * columnSize + j;
                if(index < bitmaps.size()){
                    columnBitmaps.add(bitmaps.get(index));
                }
            }
            rawBitmaps.add(ConcatenateBitmapHorizontal(columnBitmaps));
        }
        return ConcatenateBitmapVertical(rawBitmaps);
    }

    // 拼接足迹图片的九宫格
    public static Bitmap ConcatenateFootprintBitmapGrid(RecordData recordData){
        int gridSize = 3;
        if(recordData.getPicCount() == 1)
            gridSize = 1;
        else if(recordData.getPicCount() <= 4)
            gridSize = 2;

        ArrayList<Bitmap> bitmaps = new ArrayList<>();
        for (int i = 0; i < recordData.getPicCount(); ++i){
            bitmaps.add(recordData.getBitmap(i));
        }

        return ConcatenateBitmapGrid(bitmaps, (recordData.getPicCount() + gridSize - 1) / gridSize, gridSize);
    }
}
